package com.libraryApp.service.impl;

import com.libraryApp.appVo.AppBorrowingVo;
import com.libraryApp.entity.BookDo;
import com.libraryApp.entity.UserDo;
import com.libraryApp.mapper.AppBookMapper;
import com.libraryApp.service.AppBookService;
import com.libraryManage.commons.utils.MVCUtil;
import com.libraryManage.commons.utils.PageUtil;
import com.libraryManage.commons.utils.ResponseEntity;
import com.libraryManage.commons.utils.SimpleResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @CreateDate 2021-03-14
 * @Author yufang
 */
@Service
public class AppBookServiceImpl implements AppBookService {

    @Autowired
    AppBookMapper appBookMapper;

    @Override
    public ResponseEntity<List<BookDo>> searchBook(int pageNumber, int bookCategoryId, String keyword,String searchType) {

        BookDo bookDo = new BookDo();
        bookDo.setPageNum(pageNumber);
        bookDo.setPageSize(7);
        PageUtil.startPage(bookDo);
        List<BookDo> list = appBookMapper.searchBook(bookCategoryId,keyword,searchType);
        return PageUtil.tranferPage(list);

    }

    /**
     * 获取热门图书
     * @return
     */
    @Override
    public ResponseEntity<List<BookDo>> getHotBook() {

        List<BookDo> list = appBookMapper.getHotBook();
        return MVCUtil.response(list);

    }

    /**
     * 获取图书详情
     * @param bookId
     * @return
     */
    @Override
    public ResponseEntity<BookDo> getBookData(int bookId) {

        BookDo bookDo = appBookMapper.getBookData(bookId);
        return MVCUtil.response(bookDo);

    }

    /**
     * 加入书架
     * @param bookId
     * @return
     */
    @Override
    public SimpleResp addBookshelf(int bookId, UserDo userDo) {

        int state = appBookMapper.addBookshelf(bookId,userDo.getUserId());
        if(state == 0){
            return new SimpleResp(500,"操作失败，请稍后再试！");
        }
        return new SimpleResp();

    }

    /**
     * 预约借阅图书
     * @param userId
     * @param bookId
     * @return
     */
    @Override
    public synchronized SimpleResp borrowBook(Long userId, int bookId) {

        //1.查询用户是否还有其他预约纪录
        int isBorrowing = appBookMapper.getBorrowCount(userId,bookId);
        // 2.如果有则禁止用户继续借阅
        if(isBorrowing >= 1){
            return new SimpleResp(333,"还有预约纪录，禁止继续预约");
        }
        // 3.如果没有则让用户预约
        if(isBorrowing == 0){
            AppBorrowingVo appBorrowingVo = new AppBorrowingVo();
            appBorrowingVo.setBookId(bookId);
            appBorrowingVo.setUserId(userId);
            appBorrowingVo.setStartTime(new Date());
            appBorrowingVo.setIsOver(0);
            appBorrowingVo.setIsTrue(0);
            //查看是否还有库存
            int stock = appBookMapper.getBookStock(bookId);
            if(stock != 0){
                appBookMapper.addBorrow(appBorrowingVo);
                appBookMapper.updateBookStock(bookId);
                return new SimpleResp();
            }
            return new SimpleResp(333,"图书以被借完");

        }

        return new SimpleResp(333,"系统繁忙，请稍后再试!");

    }

}
